package com.woniu.his.dms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.dms.common.DmsResultCode;
import com.woniu.his.dms.common.dmsStatusEnum.DmsStatusCode;
import com.woniu.his.dms.infra.model.CaseModel;
import com.woniu.his.dms.infra.model.CaseModelCatalog;
import com.woniu.his.dms.infra.mapper.CaseModelCatalogMapper;
import com.woniu.his.dms.service.CaseModelCatalogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.dms.service.CaseModelService;
import com.woniu.his.dms.service.dto.CaseModelCatalogDto;
import com.woniu.his.dms.service.param.AddCaseModelCatalogParam;
import com.woniu.his.exception.HisException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 病历模板目录 服务实现类
 * </p>
 *
 * @author ZJH
 * @since 2022-11-27
 */
@Service
public class CaseModelCatalogServiceImpl extends ServiceImpl<CaseModelCatalogMapper, CaseModelCatalog> implements CaseModelCatalogService {

    @Resource
    private CaseModelService caseModelService;

    @Override
    public Boolean addCaseModelCatalogService(AddCaseModelCatalogParam param) {
        CaseModelCatalog caseModelCatalog = BeanUtil.copyProperties(param, CaseModelCatalog.class);
        //判断是否为根目录
        if(param.getParentId()==0){
            caseModelCatalog.setLevelCmc(0);
        }else {
            List<CaseModel> caseModels = caseModelService.list(new QueryWrapper<CaseModel>().eq("catalog_id", param.getParentId()));
            if(caseModels!=null||caseModels.size()!=0) throw new HisException(DmsResultCode.THIS_CASE_MODEL_CATALOG_CANNOT_ADD_CATALOG);
            CaseModelCatalog modelCatalog = getById(param.getParentId());
            caseModelCatalog.setLevelCmc(modelCatalog.getLevelCmc()+1);
        }
        caseModelCatalog.setStatusCaseCatalog(DmsStatusCode.DMS_STATUS_ON.getCode());
        caseModelCatalog.setCreateTime(DateUtil.formatDateTime(new Date()));
        save(caseModelCatalog);
        return true;
    }

    @Override
    public Boolean deleteCaseModelCatalogById(Long id) {
        CaseModelCatalog catalog = getById(id);
        if(catalog == null) throw new HisException(DmsResultCode.CASE_MODEL_CATALOG_NOT_EXISTS);
        List<CaseModelCatalog> catalogs = list(new QueryWrapper<CaseModelCatalog>().eq("parent_id", id));
        if(catalogs==null||catalogs.size()==0) {
            List<CaseModel> caseModels = caseModelService.list(new QueryWrapper<CaseModel>().eq("catalog_id", id));
            if(caseModels==null||caseModels.size()==0) removeById(id);
            else throw new HisException(DmsResultCode.THIS_CASE_MODEL_CATALOG_EXISTS_CASE_MODEL);
        }else {
            List<Long> catIds = catalogs.stream().map(c -> c.getId()).collect(Collectors.toList());
            for (Long catId : catIds) {
                deleteCaseModelCatalogById(catId);
            }
            for (Long catId : catIds) {
                deleteCatalogById(catId);
            }
        }
        return true;
    }

    @Override
    public Boolean updateCaseModelCatalogName(Long id, String catalogName) {
        CaseModelCatalog catalog = getById(id);
        if(catalog==null) throw new HisException(DmsResultCode.CASE_MODEL_CATALOG_NOT_EXISTS);
        catalog.setCatalogName(catalogName);
        updateById(catalog);
        return true;
    }

    @Override
    public List<CaseModelCatalogDto> queryCaseModelCatalog() {
        List<CaseModelCatalog> modelCatalogs = list();
        List<CaseModelCatalogDto> dtos = BeanUtil.copyToList(modelCatalogs, CaseModelCatalogDto.class);
        List<CaseModelCatalogDto> rootCatalogs = dtos.stream().filter(d -> d.getLevelCmc() == 0).collect(Collectors.toList());
        List<CaseModelCatalogDto> catalogDtos = queryCatalog(dtos, rootCatalogs);
        return catalogDtos;
    }

    private List<CaseModelCatalogDto> queryCatalog(List<CaseModelCatalogDto> dtos,List<CaseModelCatalogDto> rootCatalogs){
        for (CaseModelCatalogDto rootCatalog : rootCatalogs) {
            rootCatalog.setCatalogTreeDtos(new ArrayList<>());
            for (CaseModelCatalogDto dto : dtos) {
                if(dto.getParentId()==rootCatalog.getId()){
                    rootCatalog.getCatalogTreeDtos().add(dto);
                }
            }
            if(rootCatalog.getCatalogTreeDtos().size()>0){
                queryCatalog(dtos,rootCatalog.getCatalogTreeDtos());
            }
        }
        return rootCatalogs;
    }

    private void deleteCatalogById(Long id){
        List<CaseModelCatalog> catalogs = list(new QueryWrapper<CaseModelCatalog>().eq("parent_id", id));
        if(catalogs==null||catalogs.size()==0){
            removeById(id);
        }else {
            List<Long> ids = catalogs.stream().map(c -> c.getId()).collect(Collectors.toList());
            List<CaseModelCatalog> cats = list(new QueryWrapper<CaseModelCatalog>().in("parent_id", ids));
            removeByIds(ids);
            for (CaseModelCatalog cat : cats) {
                deleteCatalogById(cat.getId());
            }
        }
    }
}
